package com.zzyq.pc.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.codec.binary.Base64;

import com.zzyq.pc.common.enums.Channel;
import com.zzyq.pc.mongodb.model.MongodbDate;
import com.zzyq.pc.web.report.model.Report;

import freemarker.template.Configuration;
import freemarker.template.Template;

public class WordUtils {
	private static Configuration configuration = null;
	private static Map<String, Template> allTemplates = null;
	private static Map<String, Template> testTemplates = null;


	static {
		configuration = new Configuration();
		configuration.setDefaultEncoding("utf-8");
		configuration.setClassForTemplateLoading(WordUtils.class, "/com/zzyq/pc/resume");
		allTemplates = new HashMap<String, Template>();
		testTemplates = new HashMap<String, Template>();
		try {
			allTemplates.put("resume", configuration.getTemplate("resume.ftl"));
			testTemplates.put("test", configuration.getTemplate("test.ftl"));
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	private WordUtils() {
		throw new AssertionError();
	}

	public static File createDoc(Map<String, Object> root, String type) {
		String name = "temp" + (int) (Math.random() * 100000) + ".docx";
		File f = new File(name);
		Template t = allTemplates.get(type);
		try {
			Writer w = new OutputStreamWriter(new FileOutputStream(f), "utf-8");
			t.process(root, w);
			w.close();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return f;
	}

	public static File createDoc2(Map<String, Object> root, String type) {
		String name = "temp" + (int) (Math.random() * 100000) + ".docx";
		File f = new File(name);
		Template t = testTemplates.get(type);
		try {
			Writer w = new OutputStreamWriter(new FileOutputStream(f), "utf-8");
			t.process(root, w);
			w.close();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return f;
	}

	public static List<Map<String, Object>> ListFitter(List<MongodbDate> butelist) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		List<Map<String, Object>> list = new ArrayList<>();
		for (MongodbDate bute : butelist) {
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put("datetitle", bute.getTitle());
			dataMap.put("websiteName", bute.getWebsiteName());
			dataMap.put("autro", bute.getAuthor());
			dataMap.put("releaseTime", dateFormat.format(bute.getReleaseTime()));
			dataMap.put("reprintNumb", bute.getReprintNumb());
			if (bute.getArticleType().equals(Channel.NEWS.getName())) {
				dataMap.put("xinji", 2);// 四星
			} else if (bute.getReprintNumb() >= 21) {
				dataMap.put("xinji", 1);
			} else if (bute.getReprintNumb() >= 16) {
				dataMap.put("xinji", 2);
			} else if (bute.getReprintNumb() >= 11) {
				dataMap.put("xinji", 3);
			} else if (bute.getReprintNumb() >= 6) {
				dataMap.put("xinji", 4);
			} else if (bute.getReprintNumb() >= 0) {
				dataMap.put("xinji", 5);
			}
			list.add(dataMap);
		}

		return list;
	}

	public static List<Map<String, Object>> MomFitter(List<Report> reportlist) {
		List<Map<String, Object>> list = new ArrayList<>();
		for (Report report : reportlist) {
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put("total", report.getTotal());
			dataMap.put("totalCompare", report.getTotalCompare() + "%");
			dataMap.put("positiveNum", report.getPositiveNum());
			dataMap.put("positiveCompare", report.getPositiveCompare() + "%");
			dataMap.put("negativeNum", report.getNegativeNum());
			dataMap.put("negativeCompare", report.getNegativeCompare() + "%");
			dataMap.put("day", report.getReportName());
			list.add(dataMap);
		}
		return list;
	}

	public static String getImageStr(String imgFile) {
		InputStream in = null;
		byte[] data = null;
		try {
			in = new FileInputStream(imgFile);
			data = new byte[in.available()];
			in.read(data);
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		String imageCodeBase64 = Base64.encodeBase64String(data);
		return imageCodeBase64;
	}

}
